/*
 * Ethan Levine
 * 
 * The contents of this file are subject to the GNU General Public
 * License (GPL) v2.  The full contents of this license can be
 * found at Mathma's website, http://www.codeplex.com/mathma/license
 */

package mathma.core.calc;

import mathma.core.form.SuspendedExpression;
import mathma.core.real.Real;
import mathma.core.form.UnknownSuspensionException;
import mathma.core.expr.Variable;
import mathma.core.expr.Expression;
import mathma.core.real.InfiniteReal;
import mathma.core.expr.VariableMapping;

import java.util.ArrayList;
import java.util.List;

/**
 * @since 0.1.4
 * @author Ethan Levine
 */
public class DirectedLimitExpression extends SuspendedExpression<Real, Real> {

    private Expression<Real> operand;
    private Variable<Real> var;
    private Real approach;
    private boolean posApproach;
    
    public DirectedLimitExpression(Expression<Real> operand,
            Variable<Real> var, Real approach, boolean posApproach) {
        super(Real.Type);
        this.operand = operand;
        this.var = var;
        this.approach = approach;
        this.posApproach = posApproach;
    }
    
    public Expression<Real> getOperand() {
        return operand;
    }
    
    public Variable<Real> getVariable() {
        return var;
    }
    
    public Real getApproach() {
        return approach;
    }
    
    public boolean isApproachPositive() {
        return posApproach;
    }
    
    @Override
    public Expression<Real> calculate() throws UnknownSuspensionException {
        // TODO - implement
        return null;
    }
    
    @Override
    public DirectedLimitExpression newInstance(List<Expression<Real>> operands) {
        if (operands.size() == 4
                && Variable.class.isAssignableFrom(operands.get(1).getClass())
                && Real.class.isAssignableFrom(operands.get(2).getClass())
                && Real.class.isAssignableFrom(operands.get(3).getClass())) {
            return new DirectedLimitExpression(operands.get(0),
                    (Variable<Real>) operands.get(1),
                    (Real) operands.get(2),
                    ((Real) operands.get(3)).isPositive());
        } else {
            throw new IllegalArgumentException(
                    "Improper arguments to DirectedLimitExpression.newInstance" +
                    "(List<Expression<Value>>).");
        }
    }
    
    @Override
    public List<Expression<Real>> getOperands() {
        List<Expression<Real>> operands = new ArrayList<Expression<Real>>();
        operands.add(operand);
        operands.add(var);
        operands.add(approach);
        if (posApproach) {
            operands.add(InfiniteReal.POSITIVE_INFINITY);
        } else {
            operands.add(InfiniteReal.NEGATIVE_INFINITY);
        }
        return operands;
    }

    @Override
    public Expression<Real> differentiate(Variable<Real> var) {
        try {
            return calculate().differentiate(var);
        } catch (UnknownSuspensionException e) {
            return new DerivativeExpression(this, var);
        }
    }
}
